package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.eventbus.PutForwardEventBus;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerShopKeepFragmentComponent;
import com.wgts.wgts.dagger.module.ShopKeepFragmentModule;
import com.wgts.wgts.localbean.ShopKeepData;
import com.wgts.wgts.mvp.presenter.IShopKeepFragmentPersenter;
import com.wgts.wgts.mvp.view.IShopKeepFragmentView;
import com.wgts.wgts.mvp.view.activity.homapge.HomePageActionEntryAdapter;
import com.wgts.wgts.mvp.view.activity.store.StoreActivity;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.netmodle.resp.ShopKeepTaskEntity;
import com.wgts.wgts.netmodle.resp.ShopKeeperEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

public class ShopKeepFragment extends LazyBaseFragment implements IShopKeepFragmentView {

    private static final Double THOUSAND = 10000d;


    Unbinder unbinder;


    @Inject
    IShopKeepFragmentPersenter persenter;
    @BindView(R.id.tv_mounth_money)
    TextView tvMounthMoney;
    @BindView(R.id.linear_task)
    LinearLayout linearTask;
    @BindView(R.id.img_circle_head)
    CircleImageView imgCircleHead;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.img_vip_statue)
    ImageView imgVipStatue;
    @BindView(R.id.tv_vip_state)
    TextView tvVipState;
    @BindView(R.id.linear_vip)
    LinearLayout linearVip;
    @BindView(R.id.img_code)
    ImageView imgCode;
    @BindView(R.id.tv_mobi)
    TextView tvMobi;
    @BindView(R.id.linear_forward)
    LinearLayout linearForward;
    @BindView(R.id.linear_asset)
    LinearLayout linearAsset;
    @BindView(R.id.tv_wait)
    TextView tvWait;
    @BindView(R.id.tv_wait_util)
    TextView tvWaitUtil;
    @BindView(R.id.tv_accumulative)
    TextView tvAccumulative;
    @BindView(R.id.tv_accumulative_util)
    TextView tvAccumulativeUtil;
    @BindView(R.id.tv_put_forward)
    TextView tvPutForward;
    @BindView(R.id.tv_put_forward_util)
    TextView tvPutForwardUtil;
    @BindView(R.id.tv_extension)
    TextView tvExtension;
    @BindView(R.id.tv_extension_util)
    TextView tvExtensionUtil;
    @BindView(R.id.tv_commission)
    TextView tvCommission;
    @BindView(R.id.tv_commission_util)
    TextView tvCommissionUtil;
    @BindView(R.id.linear_operating)
    LinearLayout linearOperating;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_order_util)
    TextView tvOrderUtil;
    @BindView(R.id.tv_transaction)
    TextView tvTransaction;
    @BindView(R.id.tv_transaction_util)
    TextView tvTransactionUtil;
    @BindView(R.id.tv_profit)
    TextView tvProfit;
    @BindView(R.id.tv_profit_util)
    TextView tvProfitUtil;
    @BindView(R.id.linear_my_extension)
    LinearLayout linearMyExtension;
    @BindView(R.id.tv_friends)
    TextView tvFriends;
    @BindView(R.id.tv_friends_util)
    TextView tvFriendsUtil;
    @BindView(R.id.tv_fans)
    TextView tvFans;
    @BindView(R.id.tv_fans_util)
    TextView tvFansUtil;
    @BindView(R.id.linear_invitation_friend)
    LinearLayout linearInvitationFriend;
    @BindView(R.id.user_task)
    LinearLayout userTask;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.tv_shop_info)
    TextView tvShopInfo;
    @BindView(R.id.img_head)
    ImageView imgHead;

    private QrCodeDialogFragment fragment;

    private ShopKeeperEntity entity;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见
        if (isVisibleToUser) {
            persenter.initData();
            persenter.initTask();
            persenter.initMohu();
        }
    }


    @Override
    protected void loadData() {

    }

    @Override
    public void init() {
        DaggerShopKeepFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .shopKeepFragmentModule(new ShopKeepFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.shop_keep_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        initView();
        return view;
    }

    private void initView() {

        tvAccumulative.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvCommission.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvExtension.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvFans.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvFriends.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvOrder.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvProfit.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvPutForward.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvTransaction.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvWait.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));
        tvMounthMoney.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/din.ttf"));


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycleView.setLayoutManager(linearLayoutManager);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();

    }


    @OnClick({R.id.linear_asset, R.id.tv_wait, R.id.linear_task, R.id.linear_operating, R.id.linear_forward, R.id.img_code, R.id.linear_my_extension, R.id.tv_shop_info, R.id.linear_invitation_friend})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_asset:
                jumpActivity(AssetDetailActivity.class);
                break;
            case R.id.tv_wait:
                break;
            case R.id.linear_task:
                jumpActivity(AdvancedTaskActivity.class);
                break;
            case R.id.linear_operating:
                jumpActivity(OperatingDataActivity.class);
                break;
            case R.id.linear_forward:
                jumpActivity(PutForwardActivity.class, "MOBI", tvMobi.getText().toString());
                break;
            case R.id.img_code:
                fragment.show(getChildFragmentManager(), "");
                break;
            case R.id.linear_my_extension:
                extension();
                break;
            case R.id.tv_shop_info:
                Intent intent = new Intent(getContext(), StoreActivity.class);
                intent.putExtra(StoreActivity.USER_TYPE, "self");
                startActivity(intent);
                break;
            case R.id.linear_invitation_friend:
                Intent intent1 = new Intent(getContext(), UpgradeShopKeeperActivity.class);
                //intent.putExtra(StoreActivity.USER_TYPE, StoreActivity.USER_MY);
                startActivity(intent1);
                break;
        }
    }

    private void extension() {

        Intent intent = new Intent(getContext(), MyExtensionActivity.class);
        if (entity != null) {
            ShopKeepData shopKeepData = new ShopKeepData();
            //shopKeepData.code =
            shopKeepData.shopkeeperYQCode = entity.shopkeeperYQCode;
            shopKeepData.qCodeUrl = entity.shopkeeperQRCode;
            shopKeepData.shopAvatar = entity.shopAvatar;
            shopKeepData.userName = entity.shopName;
            shopKeepData.userState = entity.userRole;
            intent.putExtra(MyExtensionActivity.SHOPKEEP, shopKeepData);
        }
        startActivity(intent);
    }

    private void jumpActivity(Class activity) {
        Intent intent = new Intent(getContext(), activity);
        startActivity(intent);
    }

    private void jumpActivity(Class activity, String paramName, String param) {
        Intent intent = new Intent(getContext(), activity);
        intent.putExtra(paramName, param);
        startActivity(intent);
    }

    @Override
    public void initResult(ShopKeeperEntity entity) {
        this.entity = entity;

        fragment = QrCodeDialogFragment.getInstance(entity.shopkeeperQRCode, entity.shopkeeperYQCode, entity.shopAvatar, "");

        tvUserName.setText(entity.shopName);

        switch (Integer.valueOf(entity.userRole)) {
            case 0:
                imgVipStatue.setImageResource(R.mipmap.ic_store_member_white);

                break;
            case 1:
                imgVipStatue.setImageResource(R.mipmap.ic_store_member_white);

                break;
            case 2:
                imgVipStatue.setImageResource(R.mipmap.ic_store_store_white);

                break;
            case 3:
                imgVipStatue.setImageResource(R.mipmap.ic_data_manager_white);

                break;
            case 4:
                imgVipStatue.setImageResource(R.mipmap.ic_data_rudder_white);

                break;
        }

        if (entity.userRole.equals("0")) {
            tvVipState.setText("内部员工");
        } else if (entity.userRole.equals("1")) {
            tvVipState.setText("普通会员");
        } else if (entity.userRole.equals("2")) {
            tvVipState.setText("店主");
        } else if (entity.userRole.equals("3")) {
            tvVipState.setText("掌柜子");
        } else if (entity.userRole.equals("4")) {
            tvVipState.setText("分舵主");
        }

        tvMobi.setText(entity.availableBalance);

        MoHuGlide.getInstance().glideImg(getContext(), entity.shopAvatar, imgCircleHead);

        DecimalFormat df = new DecimalFormat("#.##");

        if (Double.valueOf(entity.pendingSettlement) / THOUSAND < 1) {
            tvWaitUtil.setVisibility(View.INVISIBLE);
            tvWait.setText(entity.pendingSettlement);
        } else {
            tvWaitUtil.setVisibility(View.VISIBLE);
            tvWait.setText(df.format(Double.valueOf(entity.pendingSettlement) / THOUSAND));
        }

        if (Double.valueOf(entity.totalSettlement) / THOUSAND < 1) {
            tvAccumulativeUtil.setVisibility(View.INVISIBLE);
            tvAccumulative.setText(entity.totalSettlement);
        } else {
            tvAccumulativeUtil.setVisibility(View.VISIBLE);
            tvAccumulative.setText(df.format(Double.valueOf(entity.totalSettlement) / THOUSAND));
        }


        if (Double.valueOf(entity.totalWithdraw) / THOUSAND < 1) {
            tvPutForwardUtil.setVisibility(View.INVISIBLE);
            tvPutForward.setText(entity.totalWithdraw);
        } else {
            tvPutForwardUtil.setVisibility(View.VISIBLE);
            tvPutForward.setText(df.format(Double.valueOf(entity.totalWithdraw) / THOUSAND));
        }


        if (Double.valueOf(entity.totalSpreadAward) / THOUSAND < 1) {
            tvExtensionUtil.setVisibility(View.INVISIBLE);
            tvExtension.setText(entity.totalSpreadAward);
        } else {
            tvExtensionUtil.setVisibility(View.VISIBLE);
            tvExtension.setText(df.format(Double.valueOf(entity.totalSpreadAward) / THOUSAND));
        }


        if (Double.valueOf(entity.totalCommissionProfit) / THOUSAND < 1) {
            tvCommissionUtil.setVisibility(View.INVISIBLE);
            tvCommission.setText(entity.totalCommissionProfit);
        } else {
            tvCommissionUtil.setVisibility(View.VISIBLE);
            tvCommission.setText(df.format(Double.valueOf(entity.totalCommissionProfit) / THOUSAND));
        }

        tvMounthMoney.setText(entity.monthTradeMoney);

        if (Double.valueOf(entity.todayOrderNum) / THOUSAND < 1) {
            tvOrderUtil.setVisibility(View.INVISIBLE);
            tvOrder.setText(entity.todayOrderNum);
        } else {
            tvOrderUtil.setVisibility(View.VISIBLE);
            tvOrder.setText(df.format(Double.valueOf(entity.todayOrderNum) / THOUSAND));
        }


        if (Double.valueOf(entity.todayTradeMoney) / THOUSAND < 1) {
            tvTransactionUtil.setVisibility(View.INVISIBLE);
            tvTransaction.setText(entity.todayTradeMoney);
        } else {
            tvTransactionUtil.setVisibility(View.VISIBLE);
            tvTransaction.setText(df.format(Double.valueOf(entity.todayTradeMoney) / THOUSAND));
        }

        if (Double.valueOf(entity.todayProfit) / THOUSAND < 1) {
            tvProfitUtil.setVisibility(View.INVISIBLE);
            tvProfit.setText(entity.todayProfit);
        } else {
            tvProfitUtil.setVisibility(View.VISIBLE);
            tvProfit.setText(df.format(Double.valueOf(entity.todayProfit) / THOUSAND));
        }

        if (Double.valueOf(entity.indirectFans) / THOUSAND < 1) {
            tvFriendsUtil.setVisibility(View.INVISIBLE);
            tvFriends.setText(entity.indirectFans);
        } else {
            tvFriendsUtil.setVisibility(View.VISIBLE);
            tvFriends.setText(df.format(Double.valueOf(entity.indirectFans) / THOUSAND));
        }

        if (Double.valueOf(entity.directFans) / THOUSAND < 1) {
            tvFansUtil.setVisibility(View.INVISIBLE);
            tvFans.setText(entity.directFans);
        } else {
            tvFansUtil.setVisibility(View.VISIBLE);
            tvFans.setText(df.format(Double.valueOf(entity.directFans) / THOUSAND));
        }


    }

    @Override
    public void initTaskResult(List<ShopKeepTaskEntity> entities) {


        userTask.removeAllViews();

        for (ShopKeepTaskEntity entity : entities) {


            View view = LayoutInflater.from(getContext()).inflate(R.layout.advanced_tast_view_holder, null, false);
            LinearLayout linearLayout = view.findViewById(R.id.linear_unfinished);
            TextView tvTask = view.findViewById(R.id.tv_task);
            LinearLayout linearLock = view.findViewById(R.id.linear_lock);
            linearLock.setVisibility(View.GONE);
            linearLayout.setVisibility(View.VISIBLE);
            tvTask.setText(entity.taskName);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO
                    Intent intent = new Intent(getContext(), UpgradeShopKeeperActivity.class);
                    startActivity(intent);
                }
            });

            userTask.addView(view);
        }
    }

    @Override
    public void initMohuModel(List<AppModleEntity> appModleEntities) {
        for (AppModleEntity entity : appModleEntities) {
            recycleView.setAdapter(new HomePageActionEntryAdapter(getContext(), entity.result));
        }
    }

    @Override
    public void onError(int status, String msg) {
        MoHuToast.showToast(msg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PutForwardEventBus event) {
       persenter.initData();
    };
}
